package com.lut.teacher.teacherservice.service.impl;

import com.lut.teacher.teacherservice.entity.TeacherInfoEntity;
import com.lut.teacher.teacherservice.entity.TeacherTitleEntity;
import com.lut.teacher.teacherservice.entity.vo.TeacherEductionVo;
import com.lut.teacher.teacherservice.service.TeacherInfoService;
import com.lut.teacher.teacherservice.utils.PageUtils;
import com.lut.teacher.teacherservice.utils.Query;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.lut.teacher.teacherservice.dao.TeacherEductionDao;
import com.lut.teacher.teacherservice.entity.TeacherEductionEntity;
import com.lut.teacher.teacherservice.service.TeacherEductionService;
import org.springframework.util.StringUtils;


@Service("teacherEductionService")
public class TeacherEductionServiceImpl extends ServiceImpl<TeacherEductionDao, TeacherEductionEntity> implements TeacherEductionService {
    @Autowired
    private TeacherInfoService teacherInfoService;
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<TeacherEductionEntity> page = this.page(
                new Query<TeacherEductionEntity>().getPage(params),
                new QueryWrapper<TeacherEductionEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public PageUtils queryEduction(Map<String, Object> params) {
        QueryWrapper<TeacherInfoEntity> wrapper1 = new QueryWrapper<>();
        QueryWrapper<TeacherEductionEntity> wrapper=new QueryWrapper<>();

        String key= (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper1.eq("name", key);
            TeacherInfoEntity one = teacherInfoService.getOne(wrapper1);
            if(!StringUtils.isEmpty(one)){
                wrapper.eq("teacher_id",one.getId());
            }else {
                wrapper.eq("teacher_id","nnul");
                IPage<TeacherEductionEntity> page1 = this.page(
                        new Query<TeacherEductionEntity>().getPage(params),
                        wrapper

                );
                return new PageUtils(page1);
            }
        }
        IPage<TeacherEductionEntity> page = this.page(
                new Query<TeacherEductionEntity>().getPage(params),
                wrapper

        );
        PageUtils pageUtils = new PageUtils(page);
        List<TeacherEductionEntity> records = page.getRecords();
        List<TeacherEductionVo> collect = records.stream().map(item -> {
            TeacherEductionVo teacherEductionVo = new TeacherEductionVo();
            BeanUtils.copyProperties(item, teacherEductionVo);
            QueryWrapper<TeacherInfoEntity> wrapper2 = new QueryWrapper<>();
            wrapper2.eq("id",item.getTeacherId());
            TeacherInfoEntity one = teacherInfoService.getOne(wrapper2);
            if(!StringUtils.isEmpty(one)){
                teacherEductionVo.setTeacherName(one.getName());
            }

            return teacherEductionVo;
        }).collect(Collectors.toList());

        pageUtils.setList(collect);

        return pageUtils;
    }

}